package dynamiclambda;

import java.lang.invoke.*;

/**
 * @author 朱海龙
 * 2025/5/29 17:00
 * @FunctionalInterface public interface MyConsumer {
 * void accept(Integer a, Integer b);
 * }
 * <p>
 * <p>
 * public class CalculateInteger {
 * <p>
 * public static void calculateIntegerAndPrintResult(Integer a, Integer b) {
 * Integer result = a + b;
 * System.out.println(result);
 * }
 * <p>
 * }
 */
public class DynamicLambdaRun {


    public static void main(String[] args) throws Throwable {


        MethodHandles.Lookup lookup          = MethodHandles.lookup();
        MethodType           rawMethodType   = MethodType.methodType(void.class, Integer.class, Integer.class);
        MethodHandle         rawMethodHandle = lookup.findStatic(CalculateInteger.class, "calculateIntegerAndPrintResult", rawMethodType);
        CallSite             callSite        = LambdaMetafactory.metafactory(lookup, "accept", MethodType.methodType(MyConsumer.class), rawMethodType, rawMethodHandle, rawMethodType);
        MethodHandle         target          = callSite.getTarget();
        MyConsumer           myConsumer      = (MyConsumer) target.invokeExact();
        myConsumer.accept(1, 2);
    }


}
